package capaDominio;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;



/**
 * 
 */

/**
 * @author AJ
 *
 */
public class utilsIO {
	
	private static final char[] HEXADECIMAL = { '0', '1', '2', '3',
        '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		
	public static DataOutputStream getDataOutputStream(Socket s) {
		// TODO Auto-generated method stub
		try {
			return new DataOutputStream(s.getOutputStream());
		} catch (IOException e) {
			System.out.println("Error IO (getDataOutputStream)!");
			e.printStackTrace();
		}
		return null;
	}
	
	public static BufferedReader getBufferedReader(Socket socket) {
		try {
			return new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} 
		catch(IOException e) {
			e.printStackTrace();
			System.out.println("Error IO (getBufferedReader)!");
		}
		return null;
	}
	
	public static PrintWriter getPrintWriter(Socket socket) {
		try {
			return new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
		} 
		catch(IOException e) {
			e.printStackTrace();
			System.out.println("Error IO (getPrintWriter)!");
		}
		return null;
	}
    
    public static String getHash(String stringToHash)  {
    	try{
	        MessageDigest md = MessageDigest.getInstance("SHA-256");
	        byte[] bytes = md.digest(stringToHash.getBytes());
	        StringBuilder sb = new StringBuilder(2 * bytes.length);
	        for (int i = 0; i < bytes.length; i++) {
	            int low = (int)(bytes[i] & 0x0f);
	            int high = (int)((bytes[i] & 0xf0) >> 4);
	            sb.append(HEXADECIMAL[high]);
	            sb.append(HEXADECIMAL[low]);
	        }
	        return sb.toString();
	    } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
	    }
	    return "";
    }


}

